package cn.uexpo.lhz.hxx;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName HuangXiangXiang
 * @Date 2020/4/25 14:13
 * @Version V1.0
 * 生成雪花算法
 * 拒绝白嫖当
 **/


@Slf4j
@Component
public class IdGeneratorSnowflake {

    private long workerId = 0;
    private long datacenter = 1;
    private Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenter);


    @PostConstruct
    public void init() {
        try {
            workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
            log.info("当前机器workerId:{}" + workerId);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("当前机器workerId 获取失败" + e);
        } finally {

        }
    }


    public synchronized long snowflakeId() {
        return snowflake.nextId();
    }


    public synchronized long snowflakeId(long workerId, long datacenter) {
        return snowflake.nextId();
    }

}
